﻿//
// eCRAM, a monthly activity report web application
// Copyright (C) 2010  Aptea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Globalization;

namespace eCRAM.Model
{
    [Table(Name="Crams")]
    public class Cram
    {
        /// <summary>
        /// Do not use this constructor! Used by LINQ to SQL.
        /// Cannot be internal :/
        /// </summary>
        public Cram()
        {
            CreationDate = DateTime.Now;
            LastModificationDate = CreationDate;
        }

        /// <summary>
        /// Creates a new CRAM given a user.
        /// </summary>
        /// <remarks>
        /// Don't forget to do a DataContext.SubmitChanges() on Cram before to create CramLine,
        /// otherwise Cram.Id won't have a proper value.
        /// </remarks>
        /// <param name="user"></param>
        public Cram(User user) : this()
        {
            UserId = user.Id;
        }

        [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public Guid Id { get; set; }

        /// <summary>
        /// The month of reporting
        /// </summary>
        [Column]
        public int MonthId { get; set; }

        /// <summary>
        /// The year of reporting
        /// </summary>
        [Column]
        public int Year { get; set; }

        [Column]
        public DateTime CreationDate { get; internal set; }

        [Column]
        public DateTime LastModificationDate { get; set; }

        [Column]
        public CramStatus Status { get; set; }

        [Column]
        public string Comment { get; set; }

        [Column]
        public Guid UserId { get; set; }

        [Association(OtherKey = "CramId")]
        private EntitySet<CramLine> _cramLines = new EntitySet<CramLine>();
        public IList<CramLine> CramLines {
            get { return _cramLines.ToList(); } //get { return _cramLines.ToList().AsReadOnly(); } //TO REVIEW             
        }

        public void AddCramLine(CramLine cramLine) {
            _cramLines.Add(cramLine);
        }

        public void DeleteCramLine(CramLine cramLine)
        {
            _cramLines.Remove(cramLine);
        }

        /// <summary>
        /// Return the month name from monthId
        /// </summary>
        /// <returns></returns>
        public string MonthName()
        {
            var formatInfoinfo = new DateTimeFormatInfo();
            string[] monthName = formatInfoinfo.MonthNames;
            return monthName[MonthId - 1];
        }

        public string GetMonthName(int idMonth)
        {
            var formatInfoinfo = new DateTimeFormatInfo();
            string[] monthName = formatInfoinfo.MonthNames;
            return monthName[idMonth - 1];
        }

    }
}
